package com.chuangshixinchen.enterprise.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chuangshixinchen.enterprise.common.Result;
import com.chuangshixinchen.enterprise.common.StatusCode;
import com.chuangshixinchen.enterprise.entity.Tb_staff;
import com.chuangshixinchen.enterprise.mapper.Tb_staffMapper;
import com.chuangshixinchen.enterprise.service.ITb_staffService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chuangshixinchen.enterprise.vo.req.StaffReqPageVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-08-16
 */
@Service
public class Tb_staffServiceImpl extends ServiceImpl<Tb_staffMapper, Tb_staff> implements ITb_staffService {

    @Override
    public Result<Page<Tb_staff>> listPage(StaffReqPageVo vo) {
        Page<Tb_staff> page = new Page<>(vo.getPageNum(), vo.getPageSize());
        LambdaQueryWrapper<Tb_staff> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasLength(vo.getUnitNumber())){
            wrapper.eq(Tb_staff::getUnitNumber, vo.getUnitNumber());
        }
        if (StringUtils.hasLength(vo.getUnitName())){
            wrapper.like(Tb_staff::getUnitName,vo.getUnitName());
        }
        if (StringUtils.hasLength(vo.getName())){
            wrapper.eq(Tb_staff::getName, vo.getName());
        }
        if (StringUtils.hasLength(vo.getIdCard())){
            wrapper.like(Tb_staff::getIdCard,vo.getIdCard());
        }
        if (vo.getStartTime()!=null){
            wrapper.ge(Tb_staff::getCreateTime,vo.getStartTime());
        }
        if (vo.getEndTime()!=null){
            wrapper.le(Tb_staff::getCreateTime,vo.getEndTime());
        }
        if (vo.getDeclarationType()!=null){
            wrapper.eq(Tb_staff::getDeclarationType,vo.getDeclarationType());
        }
        wrapper.orderByDesc(Tb_staff::getId);
        Page<Tb_staff> userPage = baseMapper.selectPage(page, wrapper);
        return new Result<>(true, StatusCode.OK,"查询成功",userPage);
    }

    @Override
    public Result<Tb_staff> getInfo(Long id) {
        Tb_staff staff = baseMapper.selectById(id);
        if (staff==null){
            return new Result<Tb_staff>(false, StatusCode.ERROR,"无此员工信息");
        }
        return new Result<Tb_staff>(true,StatusCode.OK,"查询成功",staff);
    }

    @Override
    public Result saveInfo(Tb_staff staff) {
        int changeNum = baseMapper.insert(staff);
        if (changeNum==0){
            return new Result(false,StatusCode.ERROR,"添加失败");
        }
        return new Result(true,StatusCode.OK,"添加成功");
    }

    @Override
    public Result updateInfo(Tb_staff staff) {
        int changeNum = baseMapper.updateById(staff);
        if (changeNum==0){
            return new Result(false,StatusCode.ERROR,"修改失败");
        }
        return new Result(true,StatusCode.OK,"修改成功");
    }

    @Override
    public Result removeInfo(String ids) {
        List<Long> idList = new ArrayList<>();
        List<String> idsStr = Arrays.asList(ids.split(","));
        for (int i = 0; i < idsStr.size(); i++) {
            idList.add(Long.parseLong(idsStr.get(i)));
        }
        int changeNum = baseMapper.deleteBatchIds(idList);
        if (changeNum==0){
            return new Result(false,StatusCode.ERROR,"删除失败");
        }
        return new Result(true,StatusCode.OK,"删除成功");
    }
}
